package com.itheima;

import java.util.Scanner;

public class ArrayToString {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.println("Please input the length of the int array :");
        int len = sc.nextInt();
        int[] arr = new int[len];
        System.out.println("Please input an int array:");
        for(int i = 0; i < len;i++)
            arr[i] = sc.nextInt();

        String str = ArrayToString(arr);
        System.out.println("After transfer, the string is : " );
        System.out.println(str);
    }
      public static String ArrayToString(int [] arr){

//        String str = "";
//        str += "[";
//        for(int i = 0; i < arr.length;i++){
//            if(i == arr.length - 1){
//                str += arr[i];
//            }
//            else {
//                str +=arr[i];
//                str += ", ";
//            }
//        }
//        str += "]";
//        return str;
        StringBuilder sb = new StringBuilder("");
        sb.append("[");
        for(int i = 0; i < arr.length;i++)
            if(i == arr.length - 1)
                sb.append(arr[i]);
            else
                sb.append(arr[i]).append(", ");   //链式编程思想
        sb.append("]");
        return sb.toString();

      }

}
